{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ecmwf - version\n",
    "#\n",
    "# xarry engine ({\"netcdf4\", \"scipy\", \"pydap\", \"h5netcdf\", \"pynio\", \"cfgrib\", \"zarr\"}\n",
    "# \n",
    "import eccodes\n",
    "import netCDF4\n",
    "import cfgrib\n",
    "import xarray\n",
    "\n",
    "print('eccodes version = ', eccodes.__version__)\n",
    "print('netCDF4 version = ', netCDF4.__version__)\n",
    "print('cfgrib  version = ', cfgrib.__version__)\n",
    "print('xarray  version = ', xarray.__version__)\n",
    "\n",
    "print(eccodes.codes_get_version_info())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ecmwf - xarry.engine = cfgrib 1\n",
    "#\n",
    "from fileinput import close\n",
    "import os\n",
    "import xarray as xr\n",
    "\n",
    "\n",
    "def grib1_file(fname: str):\n",
    "    \"\"\" 读取 GRIB1 文件 \"\"\"\n",
    "    if not os.path.exists(fname):\n",
    "        print(\"文件不存在 - \", fname)\n",
    "    else:\n",
    "        ds = xr.open_dataset(fname, engine='cfgrib')\n",
    "        print(ds)\n",
    "        \n",
    "    return\n",
    "\n",
    "\n",
    "# 读取grib1数据\n",
    "# fname = \"E:\\sfxData\\ECMWF\\eccodes_test_data\\era5-levels-members.grib\"\n",
    "# fname = \"E:\\sfxData\\ECMWF\\eccodes_test_data\\gep10.t12z.pgrb2af78.grib2\"\n",
    "# fname = \"E:\\sfxData\\ECMWF\\eccodes_test_data\\reduced_gaussian_model_level.grib2\"\n",
    "fname = r\"E:\\sfxData\\ECMWF\\ERA5\\20200805.grib\"\n",
    "\n",
    "grib1_file(fname)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ecmwf - xarry.engine = cfgrib 2\n",
    "#\n",
    "import os\n",
    "import xarray as xr\n",
    "\n",
    "\n",
    "def grib2_file(fname: str):\n",
    "    \"\"\" \n",
    "    读取 GRIB2 文件。\n",
    "    目前cfgrib库无法同时读取多个typeOfLevel, 因此需要根据提示筛选我们需要的数据。\n",
    "    \"\"\"\n",
    "    if not os.path.exists(fname):\n",
    "        print(\"文件不存在 - \", fname)\n",
    "    else:\n",
    "        ds = xr.open_dataset(fname, engine='cfgrib', backend_kwargs={\n",
    "            'filter_by_keys': {'typeOfLevel': 'isobaricInhPa'}})\n",
    "        print(ds)\n",
    "        pass\n",
    "\n",
    "    return\n",
    "\n",
    "\n",
    "# 读取grib2数据\n",
    "\n",
    "fname = \"E:\\\\sfxData\\\\ECMWF\\\\era5-levels-members.grib\"\n",
    "grib2_file(fname)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "cf18841ace8313d0bc088ca146c17a6c0040e82121d5cb75c0ea07172309253d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
